package com.lyz.dz.des.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.lyz.dz.des.config.ValidationGroups;
import com.lyz.dz.des.entity.ChatEntity;
import com.lyz.dz.des.pojo.status;
import com.lyz.dz.des.service.MPChatRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 聊天表 前端控制器
 * </p>
 *
 * @author <a href="https://fengwenyi.com">Erwin Feng</a>
 * @since 2021-08-21
 */
@Controller
@RequestMapping("/chatEntity")
public class ChatController extends baseController {
    @Autowired
    MPChatRepository chatRepository;
    @GetMapping("/list")
    public String bannerList(@NotNull(message = "pageSize is null ") Long pageSize, @NotNull(message = "pageCurrent is null") Long pageCurrent) throws JsonProcessingException {
        Page<ChatEntity> page = chatRepository.page(new Page<>(pageCurrent, pageSize));
        if (page.getRecords().isEmpty())
            return this.error(status.ServerCode_error,status.Server_no_data);
        else  return this.success(page);
    }
    @GetMapping("/{uid}")
    public String bannerInfo(@PathVariable("uid") String uid) throws JsonProcessingException {
        ChatEntity byId = chatRepository.getById(uid);
        if (byId == null) return this.error(status.ServerCode_error,status.Server_no_data);
        else   return  this.success(byId);
    }
    @GetMapping("/like")
    public String serachBaaner (Long  pagesize,Long pageCurrent,ChatEntity chatEntity) throws JsonProcessingException {
        QueryWrapper<ChatEntity> objectQueryWrapper = new QueryWrapper<>(chatEntity);
//        E page = chatRepository.page(new Page<>(pageCurrent, pagesize), objectQueryWrapper);
        Page<ChatEntity> page = chatRepository.page(new Page<>(pageCurrent, pagesize), objectQueryWrapper);

        if (page.getRecords().isEmpty()) return error(status.ServerCode_error,status.Server_no_data);
        else return success(page);

    }
    @PostMapping("/")
    public String addBanner(@Validated(ValidationGroups.Save.class) ChatEntity chatEntity) throws JsonProcessingException {
        boolean save = chatRepository.save(chatEntity);
        if (save) return success(chatEntity);
        else return error(status.ServerCode_network,status.Operation_faild);

    }

    @PutMapping("/")
    public String updateBanner(@Validated(ValidationGroups.Update.class) ChatEntity chatEntity) throws JsonProcessingException {
        boolean b = chatRepository.updateById(chatEntity);
        if (b) return success(chatEntity);
        else return error(status.ServerCode_error,status.Operation_faild);
    }

    @DeleteMapping("/{uid}")
    public String deleteBanner(@PathVariable("uid") String id) throws JsonProcessingException {
        boolean b = chatRepository.removeById(id);
        if (b) return success(null );
        else return error(status.ServerCode_error,status.Operation_faild);
    }
}

